﻿using BBCore.Core.Models.OracleEntity.BOM;
using BBCore.Core.Models.Samples;
using BootstrapBlazor.Components;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBCore.Application.Samples
{
    public class MasterDetailService : IMasterDetailService
    {
        private readonly IFreeSql FreeSql;
        public MasterDetailService(IFreeSql freeSql) => FreeSql = freeSql;

        public void CascadeInsertSave(ZDL_HYJ_MASTER_HEAD items, out int kingno)
        {
            kingno = FreeSql.Select<ZDL_HYJ_MASTER_HEAD>().Max(b => b.ZDL_BKC_JOB_HEAD_ID) + 1;
            items.ZDL_BKC_JOB_HEAD_ID = kingno;
            using var uow = FreeSql.CreateUnitOfWork();//开启单元测试事务
            var repo = FreeSql.GetRepository<ZDL_HYJ_MASTER_HEAD>();
            repo.UnitOfWork = uow;//手工绑定工作单元
            repo.DbContextOptions.EnableCascadeSave = true;//开启级联保存功能
            repo.Insert(items);
            uow.Commit();//事务提交,异常会自动回滚

        }

        public void CascadeUpdateSave(ZDL_HYJ_MASTER_HEAD items, int kingno)
        {
            //var repo = FreeSql.GetRepository<ZDL_BKC_JOB_HEAD>();//表ZDL_BKC_JOB_HEAD已经设置好了导航属性
            //var ret = repo.DeleteCascadeByDatabase(a => a.ZDL_BKC_JOB_HEAD_ID == kingno); //先删除主从关系表的数据
            //repo.DbContextOptions.EnableCascadeSave = true;//开启级联保存功能
            //repo.Insert(items);//然后一次性插入主从关系表的数据
            using var uow = FreeSql.CreateUnitOfWork();//开启单元测试事务
            var repository = FreeSql.GetAggregateRootRepository<ZDL_HYJ_MASTER_HEAD>();
            repository.UnitOfWork = uow;//手工绑定工作单元
            var order = repository.Select.Where(a => a.ZDL_BKC_JOB_HEAD_ID == kingno).First(); //此时已自动 Attach            
            order = items;
            repository.Update(order);
            uow.Commit();//事务提交,异常会自动回滚



        }

        public ZDL_HYJ_MASTER_HEAD? GetZDL_BKC(int kingno)
        {
            var repo = FreeSql.GetRepository<ZDL_HYJ_MASTER_HEAD>();
            var items = repo.Where(a => a.ZDL_BKC_JOB_HEAD_ID == kingno).IncludeMany(b => b.ZBJB_ITEMS).First();
            if (items == null)
            {
                return null;
            }
            else
            {
                //多表查询,获取部品描述和部品类型
                var lit = FreeSql.Select<ZDL_HYJ_MASTER_HEAD, ZDL_HYJ_DETAIL_BODY, MSI>()
                    //.LeftJoin(w => w.t1.ZDL_BKC_JOB_HEAD_ID == w.t2.ZDL_BKC_JOB_BODY_ID)
                    //.LeftJoin(w => w.t2.ITEM == w.t3.SEGMENT1 )
                    //.LeftJoin(w => w.t1.ORG_ID == w.t3.ORGANIZATION_ID)
                    .Where(w => w.t1.ZDL_BKC_JOB_HEAD_ID == w.t2.ZDL_BKC_JOB_BODY_ID)
                    .Where(w => w.t2.ITEM == w.t3.SEGMENT1)
                    .Where(w => w.t1.ORG_ID == w.t3.ORGANIZATION_ID)
                    .Where(w => w.t1.ZDL_BKC_JOB_HEAD_ID == kingno)
                    .OrderBy(w => w.t2.ITEM)
                    .ToList(w => new ZDL_HYJ_DETAIL_BODY
                    {
                        CREATED_DATE = w.t2.CREATED_DATE,
                        ITEM = w.t2.ITEM,
                        DESCRIPTION = w.t3.DESCRIPTION,
                        ITEM_TYPE = w.t3.ITEM_TYPE,
                        JOB2 = w.t2.JOB2,
                        LEVEL_ENUMS = w.t2.LEVEL_ENUMS,
                        LEVEL2 = w.t2.LEVEL2,
                        ZDL_BKC_JOB_BODY_ID = kingno
                    });
                items.ZBJB_ITEMS = lit;
                return items;
            }
        }

        public List<ZDL_HYJ_DETAIL_BODY>? GetZDL_BKC_BODY_DETAIL(ICollection<ZDL_HYJ_DETAIL_BODY> importDetail)
        {
            //方法一:ForEach笨方法
            //importDetail.ToList().ForEach(
            //   a =>
            //   {
            //       a.ITEM = a.ITEM;
            //       a.DESCRIPTION = FreeSql.Select<MSI>().Where(x => x.SEGMENT1 == a.ITEM).Where(x => x.ORGANIZATION_ID == 306).ToList(x => x.DESCRIPTION).First();
            //       a.ITEM_TYPE =   FreeSql.Select<MSI>().Where(x => x.SEGMENT1 == a.ITEM).Where(x => x.ORGANIZATION_ID == 306).ToList(x => x.ITEM_TYPE).First();
            //       a.JOB2 = a.JOB2;
            //       a.LEVEL2 = a.LEVEL2;
            //       a.LEVEL_ENUMS = a.LEVEL_ENUMS;
            //       a.CREATED_DATE = DateTime.Now;
            //   }
            // );
            //var lst = importDetail.ToList();
            //return lst;

            //方法2:场景4：内存数据嵌套
            var lst = FreeSql.Select<MSI>()
                .FromQuery(FreeSql.Select<ZDL_HYJ_DETAIL_BODY>().WithMemory(importDetail))
                .InnerJoin((a, b) => a.SEGMENT1 == b.ITEM && a.ORGANIZATION_ID == 306)
                .ToList((a, b) => new ZDL_HYJ_DETAIL_BODY
                {
                    CREATED_DATE = DateTime.Now,
                    ITEM = b.ITEM,
                    DESCRIPTION = a.DESCRIPTION,
                    ITEM_TYPE = a.ITEM_TYPE,
                    JOB2 = b.JOB2,
                    LEVEL_ENUMS = b.LEVEL_ENUMS,
                    LEVEL2 = b.LEVEL2

                });
            return lst;

            //方法3: IncludeMany ?


        }
    }
}
